package org.l.qiao.el;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/5/13.
 */
public class ELConvert {


    public void toList(Map<String, Object> map, String key, List<Expression> expressions) {
        Iterator iterator = map.entrySet().iterator();

        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();
            parseEntry(entry.getValue(), key + entry.getKey(), expressions);
        }

    }

    public void toList(List list, String key, List<Expression> expressions) {
        int index = 0;

        for (Iterator iterator = list.iterator(); iterator.hasNext();index++ ) {
            Object value = iterator.next();
            parseEntry(value, key + index, expressions);
        }

    }

    public void parseEntry(Object object, String key, List<Expression> expressions) {

        if (object instanceof Map) {
            Map map = (Map) object;
            toList(map, key + ".", expressions);
        } else if (object instanceof List) {
            List list = (List) object;
            toList(list, key + ".", expressions);
        } else {
            Expression expression = new Expression();
            expression.setKey(key);
            expression.setValue(object);
            expressions.add(expression);
        }

    }

}
